/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.wholeconsulting.pacc.persistance;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

import ar.com.wholeconsulting.pacc.model.Acero;
import ar.com.wholeconsulting.pacc.model.Aparato;
import ar.com.wholeconsulting.pacc.model.Elemento;
import ar.com.wholeconsulting.pacc.model.Envolvente;
import ar.com.wholeconsulting.pacc.model.MetodoSoldadura;
import ar.com.wholeconsulting.pacc.model.Modelo;
import ar.com.wholeconsulting.pacc.model.RadiografiaSoldadura;
import ar.com.wholeconsulting.pacc.model.Soldadura;
import ar.com.wholeconsulting.pacc.model.cabezal.CabezalConico;
import ar.com.wholeconsulting.pacc.model.cabezal.CabezalSemiEsferico;
import ar.com.wholeconsulting.pacc.model.cabezal.CabezalSemieliptico;
import ar.com.wholeconsulting.pacc.model.cabezal.CabezalTorisferico;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;

/**
 * 
 * @author pable
 */
public class ApatatosParser {

	static Multimap<String, Elemento> mapElementos = HashMultimap.create();
	static Multimap<String, Modelo> mapModelos = HashMultimap.create();
	static Multimap map = HashMultimap.create();
	static EntityManager manager;

	public static void parseConos(String line) {
		try {
			FileInputStream fstream = new FileInputStream(line);
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			while ((strLine = br.readLine()) != null) {
				CabezalConico cono = new CabezalConico();
				strLine = strLine.replaceAll(",", ".");
				strLine = strLine.replaceAll("\"", "");
				String vec[] = strLine.split(";");
				cono.setDiametroMayor(Double.parseDouble(vec[3]));
				cono.setDiametroMenor(Double.parseDouble(vec[4]));
				cono.setAltura(Double.parseDouble(vec[5]));
				try {
					String soldadura = vec[7].trim().replaceAll("-", "_").replaceAll(" ", "_");
					cono.setSoldaduraInterna(Soldadura.valueOf(soldadura));
				} catch (IllegalArgumentException ex) {
					ex.printStackTrace();
				}
				try {
					cono.setRadiografiaInterna(RadiografiaSoldadura.valueOf(vec[8].trim()));
				} catch (IllegalArgumentException ex) {
					ex.printStackTrace();
				}
				cono.setEficienciaDeJunta(Double.parseDouble(vec[9]));
				Query query = manager.createNamedQuery(Acero.FIND_BY_NOMBRE).setParameter("nombre", vec[10].trim());
				cono.setAcero((Acero) query.getSingleResult());
				try {
					String soldadura = vec[11].trim().replaceAll("-", "_").replaceAll(" ", "_");
					cono.setTipoSoldadura(Soldadura.valueOf(soldadura));
				} catch (IllegalArgumentException ex) {
					ex.printStackTrace();
				}
				try {
					cono.setMetodoSoldaduraExterna(MetodoSoldadura.fromString(vec[12].trim()));
				} catch (Exception e) {
					e.printStackTrace();
				}

				try {
					cono.setRadiografiaExterna(RadiografiaSoldadura.valueOf(vec[13].trim()));
				} catch (IllegalArgumentException ex) {
					ex.printStackTrace();
				}
				cono.setTensionAdmisible(Long.parseLong(vec[14]));
				cono.setEspesorDeCalculo(Double.parseDouble(vec[15]));
				cono.setEspesorNominal(Double.parseDouble(vec[16]));
				mapElementos.put(vec[1] + ";" + vec[2], cono);
			}

			in.close();
		} catch (Exception e) {// Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
	}

	public static void parseElipticos(String line) {
		try {
			FileInputStream fstream = new FileInputStream(line);
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			while ((strLine = br.readLine()) != null) {
				CabezalSemieliptico eliptico = new CabezalSemieliptico();
				strLine = strLine.replaceAll(",", ".");
				strLine = strLine.replaceAll("\"", "");
				String vec[] = strLine.split(";");
				eliptico.setDiametro(Double.parseDouble(vec[2]));
				eliptico.setPollera(Double.parseDouble(vec[4]));
				eliptico.setDiametroMayor(Double.parseDouble(vec[5]));
				eliptico.setAltura(Double.parseDouble(vec[6]));
				eliptico.setConstanteK(Double.parseDouble(vec[7]));
				try {
					String soldadura = vec[8].trim().replaceAll("-", "_").replaceAll(" ", "_");
					eliptico.setSoldaduraInterna(Soldadura.valueOf(soldadura));
				} catch (IllegalArgumentException ex) {
					ex.printStackTrace();
				}
				try {
					eliptico.setRadiografiaInterna(RadiografiaSoldadura.valueOf(vec[9].trim()));
				} catch (IllegalArgumentException ex) {
					ex.printStackTrace();
				}
				eliptico.setEficienciaDeJunta(Double.parseDouble(vec[10]));
				Query query = manager.createNamedQuery(Acero.FIND_BY_NOMBRE).setParameter("nombre", vec[10].trim());
				eliptico.setAcero((Acero) query.getSingleResult());
				try {
					String soldadura = vec[12].trim().replaceAll("-", "_").replaceAll(" ", "_");
					eliptico.setTipoSoldadura(Soldadura.valueOf(soldadura));
				} catch (IllegalArgumentException ex) {
					ex.printStackTrace();
				}
				try {
					eliptico.setMetodoSoldaduraExterna(MetodoSoldadura.fromString(vec[13].trim()));
				} catch (Exception e) {
					e.printStackTrace();
				}

				try {
					eliptico.setRadiografiaExterna(RadiografiaSoldadura.valueOf(vec[14].trim()));
				} catch (IllegalArgumentException ex) {
					ex.printStackTrace();
				}
				eliptico.setTensionAdmisible(Long.parseLong(vec[15]));
				eliptico.setEspesorDeCalculo(Double.parseDouble(vec[16]));
				eliptico.setEspesorNominal(Double.parseDouble(vec[17]));
				mapElementos.put(vec[1] + ";" + vec[2], eliptico);
			}

			in.close();
		} catch (Exception e) {// Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
	}

	public static void parseEnvolventes(String line) {
		try {
			FileInputStream fstream = new FileInputStream(line);
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			while ((strLine = br.readLine()) != null) {
				Envolvente envolvente = new Envolvente();
				strLine = strLine.replaceAll(",", ".");
				strLine = strLine.replaceAll("\"", "");
				String vec[] = strLine.split(";");
				envolvente.setDiametro(Double.parseDouble(vec[2]));
				envolvente.setLongitud(Double.parseDouble(vec[3]));
				Query query = manager.createNamedQuery(Acero.FIND_BY_NOMBRE).setParameter("nombre", vec[4].trim());
				envolvente.setAcero((Acero) query.getSingleResult());
				try {
					String soldadura = vec[5].trim().replaceAll("-", "_").replaceAll(" ", "_");
					envolvente.setTipoSoldadura(Soldadura.valueOf(soldadura));
				} catch (IllegalArgumentException ex) {
					ex.printStackTrace();
				}
				try {
					envolvente.setMetodoSoldadura(MetodoSoldadura.fromString(vec[6].trim()));
				} catch (Exception e) {
					e.printStackTrace();
				}

				try {
					envolvente.setRadiografia(RadiografiaSoldadura.valueOf(vec[7].trim()));
				} catch (IllegalArgumentException ex) {
					ex.printStackTrace();
				}
				envolvente.setEficienciaDeJunta(Double.parseDouble(vec[8]));
				envolvente.setTensionAdmisible(Long.parseLong(vec[9]));
				envolvente.setEspesorDeCalculo(Double.parseDouble(vec[10]));
				envolvente.setEspesorNominal(Double.parseDouble(vec[11]));
				mapElementos.put(vec[1] + ";" + vec[2], envolvente);
			}

			in.close();
		} catch (Exception e) {// Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
	}

	public static void parseEsfericos(String line) {
		try {
			FileInputStream fstream = new FileInputStream(line);
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			while ((strLine = br.readLine()) != null) {
				CabezalSemiEsferico esferico = new CabezalSemiEsferico();
				strLine = strLine.replaceAll(",", ".");
				strLine = strLine.replaceAll("\"", "");
				String vec[] = strLine.split(";");
				esferico.setDiametro(Double.parseDouble(vec[2]));
				esferico.setPollera(Double.parseDouble(vec[4]));
				try {
					String soldadura = vec[5].trim().replaceAll("-", "_").replaceAll(" ", "_");
					esferico.setSoldaduraInterna(Soldadura.valueOf(soldadura));
				} catch (IllegalArgumentException ex) {
					ex.printStackTrace();
				}
				try {
					esferico.setRadiografiaInterna(RadiografiaSoldadura.valueOf(vec[6].trim()));
				} catch (IllegalArgumentException ex) {
					ex.printStackTrace();
				}
				esferico.setEficienciaDeJunta(Double.parseDouble(vec[7]));
				Query query = manager.createNamedQuery(Acero.FIND_BY_NOMBRE).setParameter("nombre", vec[8].trim());
				esferico.setAcero((Acero) query.getSingleResult());
				try {
					String soldadura = vec[9].trim().replaceAll("-", "_").replaceAll(" ", "_");
					esferico.setTipoSoldadura(Soldadura.valueOf(soldadura));
				} catch (IllegalArgumentException ex) {
					ex.printStackTrace();
				}
				try {
					esferico.setMetodoSoldaduraExterna(MetodoSoldadura.fromString(vec[10].trim()));
				} catch (Exception e) {
					e.printStackTrace();
				}

				try {
					esferico.setRadiografiaExterna(RadiografiaSoldadura.valueOf(vec[11].trim()));
				} catch (IllegalArgumentException ex) {
					ex.printStackTrace();
				}
				esferico.setTensionAdmisible(Long.parseLong(vec[12]));
				esferico.setEspesorDeCalculo(Double.parseDouble(vec[13]));
				esferico.setEspesorNominal(Double.parseDouble(vec[14]));
				mapElementos.put(vec[1] + ";" + vec[2], esferico);
			}

			in.close();
		} catch (Exception e) {// Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
	}

	public static void parseTorisfericos(String line) {
		try {
			FileInputStream fstream = new FileInputStream(line);
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			while ((strLine = br.readLine()) != null) {
				CabezalTorisferico torisferico = new CabezalTorisferico();
				strLine = strLine.replaceAll(",", ".");
				strLine = strLine.replaceAll("\"", "");
				String vec[] = strLine.split(";");
				torisferico.setDiametro(Double.parseDouble(vec[2]));
				torisferico.setPollera(Double.parseDouble(vec[4]));
				torisferico.setL0(Double.parseDouble(vec[5]));
				torisferico.setR0(Double.parseDouble(vec[6]));
				torisferico.setL1(Double.parseDouble(vec[7]));
				// torisferico.setR1(Double.parseDouble(vec[8]));
				torisferico.setM(Double.parseDouble(vec[8]));
				try {
					String soldadura = vec[9].trim().replaceAll("-", "_").replaceAll(" ", "_");
					torisferico.setSoldaduraInterna(Soldadura.valueOf(soldadura));
				} catch (IllegalArgumentException ex) {
					ex.printStackTrace();
				}
				try {
					torisferico.setRadiografiaInterna(RadiografiaSoldadura.valueOf(vec[10].trim()));
				} catch (IllegalArgumentException ex) {
					ex.printStackTrace();
				}
				torisferico.setEficienciaDeJunta(Double.parseDouble(vec[11]));
				try {
					String soldadura = vec[12].trim().replaceAll("-", "_").replaceAll(" ", "_");
					torisferico.setTipoSoldadura(Soldadura.valueOf(soldadura));
				} catch (IllegalArgumentException ex) {
					ex.printStackTrace();
				}
				try {
					torisferico.setMetodoSoldaduraExterna(MetodoSoldadura.fromString(vec[13].trim()));
				} catch (Exception e) {
					e.printStackTrace();
				}

				try {
					torisferico.setRadiografiaExterna(RadiografiaSoldadura.valueOf(vec[14].trim()));
				} catch (IllegalArgumentException ex) {
					ex.printStackTrace();
				}
				Query query = manager.createNamedQuery(Acero.FIND_BY_NOMBRE).setParameter("nombre", vec[15].trim());
				torisferico.setAcero((Acero) query.getSingleResult());
				torisferico.setTensionAdmisible(Long.parseLong(vec[16]));
				torisferico.setEspesorDeCalculo(Double.parseDouble(vec[17]));
				mapElementos.put(vec[1] + ";" + vec[2], torisferico);
			}

			in.close();
		} catch (Exception e) {// Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}
	}

	public static void parseTapas(String line) {
		// try {
		// FileInputStream fstream = new FileInputStream(line);
		// DataInputStream in = new DataInputStream(fstream);
		// BufferedReader br = new BufferedReader(new InputStreamReader(in));
		// String strLine;
		// while ((strLine = br.readLine()) != null) {
		// CabezalTapaPlana tapa = new CabezalTapaPlana();
		// strLine = strLine.replaceAll(",", ".");
		// strLine = strLine.replaceAll("\"", "");
		// String vec[] = strLine.split(";");
		// tapa.setDiametro(Double.parseDouble(vec[2]));
		// tapa.setPollera(Double.parseDouble(vec[4]));
		// try {
		// String soldadura = vec[5].trim().replaceAll("-", "_").replaceAll(" ",
		// "_");
		// tapa.setSoldaduraInterna(Soldadura.valueOf(soldadura));
		// } catch (IllegalArgumentException ex) {
		// ex.printStackTrace();
		// }
		// try {
		// tapa.setRadiografiaInterna(RadiografiaSoldadura.valueOf(vec[6].trim()));
		// } catch (IllegalArgumentException ex) {
		// ex.printStackTrace();
		// }
		// tapa.setEficienciaDeJunta(Double.parseDouble(vec[7]));
		// Query query =
		// manager.createNamedQuery(Acero.FIND_BY_NOMBRE).setParameter("nombre",
		// vec[8].trim());
		// tapa.setAcero((Acero) query.getSingleResult());
		// try {
		// String soldadura = vec[9].trim().replaceAll("-", "_").replaceAll(" ",
		// "_");
		// tapa.setTipoSoldadura(Soldadura.valueOf(soldadura));
		// } catch (IllegalArgumentException ex) {
		// ex.printStackTrace();
		// }
		// try {
		// tapa.setMetodoSoldaduraExterna(MetodoSoldadura.fromString(vec[10].trim()));
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		//
		// try {
		// tapa.setRadiografiaExterna(RadiografiaSoldadura.valueOf(vec[11].trim()));
		// } catch (IllegalArgumentException ex) {
		// ex.printStackTrace();
		// }
		// tapa.setTensionAdmisible(Long.parseLong(vec[12]));
		// tapa.setEspesorDeCalculo(Double.parseDouble(vec[13]));
		// tapa.setEspesorNominal(Double.parseDouble(vec[14]));
		// mapElementos.put(vec[1] + ";" + vec[2], tapa);
		// }
		//
		// in.close();
		// } catch (Exception e) {// Catch exception if any
		// System.err.println("Error: " + e.getMessage());
		// }
	}

	public static Aparato parseModelos(String line) {
		Aparato aparato = new Aparato();
		line = line.replaceAll(",", ".");
		line = line.replaceAll("\"", "");
		line.split(";");

		return aparato;
	}

	public static Aparato parseAparatos(String line) {
		Aparato aparato = new Aparato();
		line = line.replaceAll(",", ".");
		line = line.replaceAll("\"", "");
		line.split(";");

		return aparato;
	}

	public static void main(String[] args) throws Exception {
		// JPAUtil util = new JPAUtil();
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("PACC");
		manager = factory.createEntityManager();
		EntityTransaction transaction = manager.getTransaction();
		transaction.begin();

		parseConos("/home/pable/NetBeansProjects/PACC/src/main/resources/base/Conos.csv");

		transaction.commit();

		manager.close();
		factory.close();
	}
}
